import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)

const state = {
	userinfo: JSON.parse(uni.getStorageSync('userinfo') || '{}'),
	isLogin: uni.getStorageSync('userinfo') ? true : false,
	orderAll: {
		notPay: [],   //未付款
		waitFa: [],   //待发货
		waitShou: [], //待收货,
		allOrder: []  //全部订单
	}
}
const getters = {
	getUserInfo(state) {
		return state.userinfo
	},
	//返回订单的信息
	getNotPay(state) {     //未付款
		return state.orderAll.notPay
	},
	getwaitFa(state) {     //待发货
		return state.orderAll.waitFa
	},
	getwaitShou(state) {     //待收货
		return state.orderAll.waitShou
	},
	getallOrder(state) {     //全部订单
		return state.orderAll.allOrder
	}
}
const mutations = {
	// 改变用户对象
	changeUserInfo(state, obj) {
		uni.setStorageSync('userinfo', JSON.stringify(obj))
		state.userinfo = obj
		state.isLogin = true
	},
	// 得到订单的信息
	getOrder(state, arr) {
		state.orderAll.notPay = [];
		state.orderAll.allOrder = [];
		state.orderAll.waitFa = [];
		state.orderAll.waitShou = [];
		for(let i = 0; i < arr.length; i++) {
			if(arr[i].status == 0) {  //未付款
				state.orderAll.notPay.push(arr[i])
			}
			if(arr[i].status == 1) {   //待发货
				state.orderAll.waitFa.push(arr[i])
			}
			if(arr[i].status == 2) {   //待收货
				state.orderAll.waitShou.push(arr[i])
			}
			state.orderAll.allOrder.push(arr[i])     //全部订单
		}
	}
}
const actions = {
	//得到订单的信息
	getOrderData({commit}) {
		uni.showLoading({
			title: '处理中...'
		})
		uniCloud.callFunction({
			name: 'order',
			data: {
				type: 'getAll'
			}
		}).then(res => {
			uni.hideLoading()
			commit('getOrder', res.result.data)
		})
	}
	
}
const store = new Vuex.Store({
	state,
	getters,
	mutations,
	actions
})

export default store